<template>
  <header class="navbar">
    <div class="nav-content">
      <div class="logo">
        <h1>🎨 AI 艺术创作</h1>
      </div>
      <nav class="nav-links">
        <a 
          href="/home" 
          :class="{ active: isRouteActive('/home') }"
        >
          首页
        </a>
        <div class="dropdown">
          <a 
            href="/painting" 
            class="dropbtn"
            :class="{ active: isArtRouteActive() }"
          >
            艺术创作
          </a>
          <div class="dropdown-content">
            <a 
              href="/painting" 
              :class="{ active: currentRoute === '/painting' }"
            >
              AI绘画
            </a>
            <a 
              href="/music" 
              :class="{ active: currentRoute === '/music' }"
            >
              AI音乐
            </a>
            <a 
              href="/writing" 
              :class="{ active: currentRoute === '/writing' }"
            >
              AI写作
            </a>
          </div>
        </div>
        <a 
          href="/community" 
          :class="{ active: isRouteActive('/community') }"
        >
          社区
        </a>
        <a 
          href="/inspiration" 
          :class="{ active: isRouteActive('/inspiration') }"
        >
          灵感启发
        </a>
      </nav>
      <div class="user-section">
        <div v-if="username" class="user-profile-container">
          <div 
            class="user-avatar" 
            @click="goToProfile" 
            @mouseenter="showUserProfile = true; cancelHideTimer()"
          >
            {{ username.charAt(0).toUpperCase() }}
          </div>
          
          <div 
            v-show="showUserProfile" 
            class="user-profile-popup"
            @mouseenter="cancelHideTimer"
            @mouseleave="startHideTimer"
          >
            <div class="profile-header">
              <div class="profile-avatar">{{ username.charAt(0).toUpperCase() }}</div>
              <div class="profile-name">{{ username }}</div>
            </div>
            <div class="profile-details">
              <div class="profile-item">
                <span class="item-label">用户ID:</span>
                <span class="item-value">{{ userInfo?.id || 'N/A' }}</span>
              </div>
              <div class="profile-item">
                <span class="item-label">注册时间:</span>
                <span class="item-value">{{ userInfo?.createdAt || 'N/A' }}</span>
              </div>
              <div class="profile-item">
                <span class="item-label">作品数量:</span>
                <span class="item-value">{{ userInfo?.workCount || 0 }}</span>
              </div>
            </div>
            <div class="profile-actions">
              <button class="profile-action-btn" @click="goToProfile">个人中心</button>
              <button class="profile-action-btn logout" @click="handleLogout">退出登录</button>
            </div>
          </div>
        </div>
        <button v-else class="login-btn" @click="goToLogin">登录</button>
      </div>
    </div>
  </header>
</template>

<script setup lang="ts">
import { ref, computed, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { logout } from '../api/auth'

interface UserInfo {
  username: string;
  [key: string]: any;
}

const router = useRouter()
const userInfo = ref<UserInfo | null>(null)
const showUserProfile = ref(false)
const hideTimeout = ref<number | null>(null)

const username = computed(() => userInfo.value?.username || '')
const currentRoute = computed(() => router.currentRoute.value.path)

// 判断当前路由是否激活
const isRouteActive = (route: string) => {
  return currentRoute.value === route || (route === '/home' && currentRoute.value === '/')
}

// 判断艺术创作菜单是否激活（子菜单选中时父菜单也激活）
const isArtRouteActive = () => {
  return ['/painting', '/music', '/writing'].includes(currentRoute.value)
}

const goToProfile = () => {
  router.push('/profile')
}

const goToLogin = () => {
  router.push('/')
}

const handleLogout = async () => {
  try {
    localStorage.removeItem('userInfo')
    userInfo.value = null
    router.push('/')
    
    try {
      await logout()
      console.log('用户已登出')
    } catch (error: any) {
      console.error('登出请求失败:', error)
    }
  } catch (error: any) {
    console.error('退出失败:', error)
    localStorage.removeItem('userInfo')
    userInfo.value = null
    router.push('/')
  }
}

const startHideTimer = () => {
  if (hideTimeout.value) {
    clearTimeout(hideTimeout.value)
  }
  hideTimeout.value = window.setTimeout(() => {
    showUserProfile.value = false
    hideTimeout.value = null
  }, 300)
}

const cancelHideTimer = () => {
  if (hideTimeout.value) {
    clearTimeout(hideTimeout.value)
    hideTimeout.value = null
  }
}

onMounted(() => {
  const storedUserInfo = localStorage.getItem('userInfo')
  if (storedUserInfo) {
    try {
      userInfo.value = JSON.parse(storedUserInfo)
    } catch (e) {
      console.error('解析用户信息失败:', e)
    }
  }
})
</script>

<style scoped>
.navbar {
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(20px);
  box-shadow: 0 4px 30px rgba(0, 0, 0, 0.08);
  position: sticky;
  top: 0;
  z-index: 100;
  width: 100%;
  border-bottom: 1px solid rgba(102, 126, 234, 0.1);
}

.nav-content {
  max-width: 1400px;
  margin: 0 auto;
  padding: 20px 48px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.logo h1 {
  font-size: 28px;
  color: #667eea;
  font-weight: 700;
  letter-spacing: 0.5px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.nav-links {
  display: flex;
  gap: 48px;
  align-items: center;
}

.nav-links a {
  color: #2c3e50;
  text-decoration: none;
  font-size: 16px;
  font-weight: 500;
  transition: all 0.3s ease;
  position: relative;
  padding: 10px 0;
  letter-spacing: 0.3px;
}

.nav-links a:hover {
  color: #667eea;
  transform: translateY(-1px);
}

.nav-links a.active {
  color: #667eea;
  font-weight: 600;
}

.nav-links a.active::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 3px;
  background: linear-gradient(90deg, #667eea 0%, #764ba2 100%);
  border-radius: 2px;
  box-shadow: 0 2px 8px rgba(102, 126, 234, 0.3);
}

/* 下拉菜单 */
.dropdown {
  position: relative;
  display: inline-block;
}

.dropdown-content {
  display: none;
  position: absolute;
  background: rgba(255, 255, 255, 0.98);
  backdrop-filter: blur(10px);
  min-width: 180px;
  box-shadow: 0 12px 32px rgba(0, 0, 0, 0.12);
  border-radius: 12px;
  z-index: 1;
  top: calc(100% + 8px);
  left: 50%;
  transform: translateX(-50%);
  padding: 12px 0;
  border: 1px solid rgba(102, 126, 234, 0.15);
}

.dropdown:hover .dropdown-content {
  display: block;
  animation: fadeInDown 0.3s ease;
}

@keyframes fadeInDown {
  from {
    opacity: 0;
    transform: translateX(-50%) translateY(-10px);
  }
  to {
    opacity: 1;
    transform: translateX(-50%) translateY(0);
  }
}

.dropdown-content a {
  color: #2c3e50;
  padding: 14px 28px;
  text-decoration: none;
  display: block;
  font-size: 15px;
  transition: all 0.3s ease;
  border-left: 3px solid transparent;
}

.dropdown-content a:hover {
  background: linear-gradient(90deg, rgba(102, 126, 234, 0.08) 0%, rgba(118, 75, 162, 0.05) 100%);
  color: #667eea;
  border-left-color: #667eea;
  padding-left: 32px;
}

.dropdown-content a.active {
  background: linear-gradient(90deg, rgba(102, 126, 234, 0.12) 0%, rgba(118, 75, 162, 0.08) 100%);
  color: #667eea;
  font-weight: 600;
  border-left-color: #667eea;
}

.dropdown:hover .dropbtn {
  color: #667eea;
}

.user-section {
  display: flex;
  align-items: center;
  gap: 12px;
  position: relative;
}

.user-avatar {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 600;
  font-size: 18px;
  cursor: pointer;
  transition: all 0.3s ease;
  box-shadow: 0 4px 12px rgba(102, 126, 234, 0.25);
}

.user-avatar:hover {
  transform: scale(1.08);
  box-shadow: 0 6px 20px rgba(102, 126, 234, 0.35);
}

.user-profile-popup {
  position: absolute;
  top: 58px;
  right: 0;
  width: 300px;
  background: white;
  border-radius: 16px;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.15);
  z-index: 1000;
  padding: 24px;
  animation: fadeIn 0.3s ease;
  border: 1px solid rgba(102, 126, 234, 0.1);
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(-10px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.profile-header {
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-bottom: 24px;
  padding-bottom: 24px;
  border-bottom: 1px solid #eee;
}

.profile-avatar {
  width: 70px;
  height: 70px;
  border-radius: 50%;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 600;
  font-size: 28px;
  margin-bottom: 14px;
  box-shadow: 0 4px 12px rgba(102, 126, 234, 0.25);
}

.profile-name {
  font-size: 19px;
  font-weight: 600;
  color: #2c3e50;
}

.profile-details {
  margin-bottom: 24px;
}

.profile-item {
  display: flex;
  justify-content: space-between;
  margin-bottom: 14px;
}

.profile-item:last-child {
  margin-bottom: 0;
}

.item-label {
  color: #6c757d;
  font-size: 14px;
}

.item-value {
  color: #2c3e50;
  font-size: 14px;
  font-weight: 500;
}

.profile-actions {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.profile-action-btn {
  padding: 12px 18px;
  border: none;
  border-radius: 8px;
  font-size: 15px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.3s ease;
  text-align: center;
  background: #f8f9fa;
  color: #2c3e50;
}

.profile-action-btn:hover {
  background: #e9ecef;
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.profile-action-btn.logout {
  background: #fff5f5;
  color: #e53e3e;
}

.profile-action-btn.logout:hover {
  background: #ffe5e5;
}

.login-btn {
  padding: 10px 28px;
  border: none;
  border-radius: 8px;
  font-size: 15px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.3s ease;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  box-shadow: 0 4px 12px rgba(102, 126, 234, 0.3);
}

.login-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(102, 126, 234, 0.4);
}

/* 响应式 */
@media (max-width: 768px) {
  .nav-content {
    padding: 16px 24px;
  }
  
  .nav-links {
    gap: 24px;
  }
  
  .nav-links a {
    font-size: 14px;
  }
  
  .logo h1 {
    font-size: 22px;
  }
}
</style>
